package com.liuyi.pay.wx;

import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.WxPayApiConfigKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * <p>
 * 微信支付配置
 * </p>
 *
 * @author Mr.Fmy
 * @since 2020-11-26
 */
@Configuration
public class WxPayConfig {

    private final WxPayBeans wxPayBeans;

    @Autowired
    public WxPayConfig(WxPayBeans wxPayBeans) {
        this.wxPayBeans = wxPayBeans;
        wxPayApiConfig();
    }

    private void wxPayApiConfig() {
        if (wxPayBeans != null && wxPayBeans.getBeans() != null) {
            wxPayBeans.getBeans().forEach(item -> {
                WxPayApiConfig config = new WxPayApiConfig();
                config.setAppId(item.getAppId());
                config.setMchId(item.getMchId());
                config.setPartnerKey(item.getPartnerKey());
                config.setDomain(item.getDomain());
                config.setCertPath(System.getProperty("user.dir") + File.separator + item.getCertPath());
                WxPayApiConfigKit.putApiConfig(config);
            });
        }
    }
}
